package com.cskaoyan.controller.back.configmanagement;


import com.cskaoyan.bean.vo.back.BaseRespVo;
import com.cskaoyan.bean.vo.back.configmanagement.FreightConfigVo;
import com.cskaoyan.bean.vo.back.configmanagement.MallConfigVo;
import com.cskaoyan.bean.vo.back.configmanagement.OrderConfigVo;
import com.cskaoyan.bean.vo.back.configmanagement.WXConfigVo;
import com.cskaoyan.service.back.configmanagement.ConfigManageService;
import com.cskaoyan.util.ValidationUtils;
import lombok.SneakyThrows;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 配置管理控制器
 *
 * @author 李然
 * @date 2022/10/12
 */
@RequestMapping("admin/config")
@RestController
public class ConfigManageController {

    @Autowired
    ConfigManageService configManageService;
    @RequiresPermissions("admin:config:mall:list")
    @GetMapping("mall")
    public BaseRespVo getConfigMall() {
        MallConfigVo mallConfigVo = configManageService.getConfigMall();
        return BaseRespVo.ok(mallConfigVo);
    }

    @SneakyThrows
    @RequiresPermissions("admin:config:mall:updateConfigs")
    @PostMapping("mall")
    public BaseRespVo postConfigMall(@RequestBody @Validated MallConfigVo mallConfigVo, BindingResult bindingResult) {
        ValidationUtils.valid(bindingResult);
        int count = configManageService.postConfigMall(mallConfigVo);
        return BaseRespVo.ok();
    }
    @RequiresPermissions("admin:config:express:list")
    @GetMapping("express")
    public BaseRespVo getConfigExpress() {
        FreightConfigVo freightConfigVo = configManageService.getConfigExpress();
        return BaseRespVo.ok(freightConfigVo);
    }

    @SneakyThrows
    @RequiresPermissions("admin:config:express:updateConfigs")
    @PostMapping("express")
    public BaseRespVo postConfigExpress(@RequestBody @Valid FreightConfigVo freightConfigVo, BindingResult bindingResult) {
        ValidationUtils.valid(bindingResult);
        int count = configManageService.postConfigExpress(freightConfigVo);
        return BaseRespVo.ok();
    }
    @RequiresPermissions("admin:config:order:list")
    @GetMapping("order")
    public BaseRespVo getConfigOrder() {
        OrderConfigVo orderConfigVo = configManageService.getConfigOrder();
        return BaseRespVo.ok(orderConfigVo);
    }

    @SneakyThrows
    @RequiresPermissions("admin:config:order:updateConfigs")
    @PostMapping("order")
    public BaseRespVo postConfigOrder(@RequestBody @Valid OrderConfigVo orderConfigVo, BindingResult bindingResult) {
        ValidationUtils.valid(bindingResult);
        int count = configManageService.postConfigOrder(orderConfigVo);
        return BaseRespVo.ok();
    }
    @RequiresPermissions("admin:config:wx:list")
    @GetMapping("wx")
    public BaseRespVo getConfigWX() {
        WXConfigVo wxConfigVo = configManageService.getConfigWX();
        return BaseRespVo.ok(wxConfigVo);
    }

    @SneakyThrows
    @RequiresPermissions("admin:config:wx:updateConfigs")
    @PostMapping("wx")
    public BaseRespVo postConfigWX(@RequestBody @Valid WXConfigVo wxConfigVo, BindingResult bindingResult) {
        ValidationUtils.valid(bindingResult);
        int count = configManageService.postConfigWX(wxConfigVo);
        return BaseRespVo.ok();
    }


}
